package crawler

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"strings"

	"github.com/axgle/mahonia"
)

var num int

func _1init() {
	url := "http://www.hydcd.com/cy/fkccy/index%s.htm"

	for i := 1; i <= 10; i++ {
		index_str := ""
		if i > 1 {
			index_str = fmt.Sprintf("%d", i)
		}

		checkurl := fmt.Sprintf(url, index_str)
		dowload(checkurl)
	}

	println(123)
}

func dowload(url string) {
	client := &http.Client{}
	req, _ := http.NewRequest("GET", url, nil)
	// 自定义Header
	req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
	req.Header.Add("Accept-Charset", "gb2312")
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("http get error", err)
		return
	}
	//函数结束后关闭相关链接
	defer resp.Body.Close()

	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		fmt.Println("read error", err)
		return
	}

	bodystr := mahonia.NewDecoder("gbk").ConvertString(string(body))

	var _bodystr, img, chengyu string
	_bodystr = bodystr
	for {
		_bodystr, img, chengyu = findInfo_kantu(&_bodystr)
		if img == "" {
			break
		}

		if strings.Index(img, "gif") >= 0 {
			continue
		}
		num++
		imgurl := urlJoin(img, url)
		print(num)
		println("," + chengyu + "," + imgurl)

		downloadImage(imgurl, fmt.Sprintf("./images/%d.png", num))
	}
}

func findInfo_kantu(body *string) (newbody string, imgurl string, chengyu string) {
	begin := "<img border=\"0\" src=\""
	index := strings.Index(*body, begin)
	if index < 0 {
		return
	}
	rightstr := (*body)[index+len(begin):]
	index = strings.Index(rightstr, "\"")
	imgurl = rightstr[:index]
	newbody = rightstr[index:]
	if strings.Index(imgurl, "gif") >= 0 {
		return
	}

	begin = ".htm\">"
	end := "</a>"

	tempstr := newbody[:8]
	if strings.Index(tempstr, "alt") >= 0 {
		begin = "alt=\""
		end = "\""
	}

	index = strings.Index(newbody, begin)
	rightstr = (newbody)[index+len(begin):]
	index = strings.Index(rightstr, end)
	chengyu = rightstr[:index]
	newbody = rightstr[index:]
	return
}
